package com.yylending.las.lending.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.Loader;
import android.view.*;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.Global;
import com.yylending.las.R;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.lending.QuotaEvent;
import com.yylending.las.main.message.MessageActivity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by laixj on 2018/1/11.
 * Copyright ©2018 www.yylending.com. All Rights Reserved.
 */

public class VideoAcceptActivity extends BaseActivity {
	private static final String TAG = VideoAcceptActivity.class.getSimpleName();

	public static final int REQUEST_CODE = 1002;

	@BindView(R.id.video_refused)
	TextView mVideoRefused;
	@BindView(R.id.video_accept)
	TextView mVideoAccept;
	@BindView(R.id.ellipsis)
	TextView mEllipsis;

	private final Handler mHandler = new MyHandler(this);
	private static int mCount = 1;
	private static final int TIME_UPDATE = 100;
	private Timer mTimer;
	private TimerTask mTimerTask;

	public static void start(Activity aty) {
		Intent i = new Intent(aty, VideoAcceptActivity.class);
		aty.startActivityForResult(i, REQUEST_CODE);
	}

	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
		super.onCreate(savedInstanceState);

		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
		this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//拍照过程屏幕一直处于高亮
		//设置手机屏幕朝向
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

		setContentView(R.layout.activity_video_accept);
		ButterKnife.bind(this);
		setTopBarVisible(false);

		EventAgent.register(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mTimerTask != null) {
			mTimerTask.cancel();
			mTimerTask = null;
		}
		mTimerTask = new TimerTask() {
			@Override
			public void run() {
				mHandler.sendEmptyMessage(TIME_UPDATE);
			}
		};
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		mTimer = new Timer();
		mTimer.schedule(mTimerTask, 0, 600);
	}

	@Override
	protected void onDestroy() {
		EventAgent.unregister(this);
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
		if (mTimerTask != null) {
			mTimerTask.cancel();
			mTimerTask = null;
		}
		mHandler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}


	@OnClick({R.id.video_refused, R.id.video_accept})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.video_refused:
				setResult(RESULT_CANCELED);
				finish();
				break;
			case R.id.video_accept:
				setResult(RESULT_OK);
				finish();
				break;
		}
	}

	private void updateEllipsis(int count) {
		switch (count) {
			case 1:
				mEllipsis.setText(".");
				break;
			case 2:
				mEllipsis.setText("..");
				break;
			case 3:
				mEllipsis.setText("...");
				break;
			case 4:
				mEllipsis.setText("");
				break;
		}
	}

	private static class MyHandler extends Handler {
		private final WeakReference<VideoAcceptActivity> mActivity;

		public MyHandler(VideoAcceptActivity activity) {
			mActivity = new WeakReference<>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			if (mActivity.get() == null) {
				return;
			}
			if (msg.what == TIME_UPDATE) {
				mActivity.get().updateEllipsis(mCount);
				if (mCount != 4) {
					mCount++;
				} else {
					mCount = 1;
				}
			}
		}
	}

	/**
	 * 关闭接听页
	 */
	public static class CloseVideoWaitEvent {

	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onCloseWaitPageEvent(VideoAcceptActivity.CloseVideoWaitEvent event) {
		setResult(RESULT_CANCELED);
		finish();
	}
}
